package cn.yeyiu.netonlinevideo.parser.video;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.yeyiu.netonlinevideo.bean.SourceData;
import cn.yeyiu.netonlinevideo.parser.Parser;
import cn.yeyiu.netonlinevideo.viewmodel.DataListViewModel;
import cn.yeyiu.util.NetUtil;
import cn.yeyiu.util.bean.MediaData;
import cn.yeyiu.util.bean.VideoInfo;

public class AiQuParser extends Parser {
    public AiQuParser(SourceData source) {
        super(source);
    }

    @Override
    public Object getBaseData(String type, int pageIndex) {
        //POST http://az730099xj222.com/App/Movie/movieList
        // {"classifyId":"4","pageNo":1,"pageSize":9,"sort":""}
        String vtype = getArgv(type, "vtype");
        JSONObject data = new JSONObject();
        data.put("classifyId", vtype);
        data.put("pageNo", pageIndex + 1);
        data.put("pageSize", source.pageSize);
        data.put("sort", "");
        String html = NetUtil.postByUrl(source.url, data, false);
        return JSONObject.parseObject(html);
    }

    @Override
    public List<MediaData> getDataList(Object data, String type) {
        JSONObject jo = (JSONObject) data;
        JSONArray array = jo.getJSONArray("data");

        List<MediaData> videoInfos = new ArrayList<>();
        MediaData mediaData;
        VideoInfo videoInfo;
        for (int i = 0; i < array.size(); ++i) {
            JSONObject ob = array.getJSONObject(i);
            videoInfo = new VideoInfo();
            mediaData = new MediaData();

            videoInfo.code = source.code;
            videoInfo.type = type;
            videoInfo.title = ob.getString("name");
            videoInfo.picSrc = ob.getString("cover");
            videoInfo.videoSrc = ob.getString("video_url");
            if (videoInfo.videoSrc == null) {
                continue;
            }
            mediaData.wrap(videoInfo);
            videoInfos.add(mediaData);
        }
        return videoInfos;
    }
}
